<script setup lang="ts">
interface AQLOption {
  label: string
  value: number
}

interface LotSizeRange {
  min: number
  max: number
  code: string
}

// 批量范围定义
const LOT_SIZE_RANGES: LotSizeRange[] = [
  { min: 2, max: 8, code: '2-8' },
  { min: 9, max: 15, code: '9-15' },
  { min: 16, max: 25, code: '16-25' },
  { min: 26, max: 50, code: '26-50' },
  { min: 51, max: 90, code: '51-90' },
  { min: 91, max: 150, code: '91-150' },
  { min: 151, max: 280, code: '151-280' },
  { min: 281, max: 500, code: '281-500' },
  { min: 501, max: 1200, code: '501-1200' },
  { min: 1201, max: 3200, code: '1201-3200' },
  { min: 3201, max: 10000, code: '3201-10000' },
  { min: 10001, max: 35000, code: '10001-35000' },
  { min: 35001, max: 150000, code: '35001-150000' },
  { min: 150001, max: 500000, code: '150001-500000' },
  { min: 500001, max: Infinity, code: '500001-up' },
]

// AQL可选值列表
const aqlOptions: AQLOption[] = [
  { label: '0.010%', value: 0.01 },
  { label: '0.015%', value: 0.015 },
  { label: '0.025%', value: 0.025 },
  { label: '0.040%', value: 0.04 },
  { label: '0.065%', value: 0.065 },
  { label: '0.10%', value: 0.1 },
  { label: '0.15%', value: 0.15 },
  { label: '0.25%', value: 0.25 },
  { label: '0.40%', value: 0.4 },
  { label: '0.65%', value: 0.65 },
  { label: '1.0%', value: 1.0 },
  { label: '1.5%', value: 1.5 },
  { label: '2.5%', value: 2.5 },
  { label: '4.0%', value: 4.0 },
  { label: '6.5%', value: 6.5 },
  { label: '10.0%', value: 10.0 },
]

// 状态管理
const state = reactive({
  lotSize: undefined as number | undefined,
  aqlValue: undefined as number | undefined,
  sampleSize: 0,
  isFullInspection: false,
})

// 零缺陷抽样表数据
const samplingTable = {
  '2-8': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': '*',
    '0.1': '*',
    '0.15': '*',
    '0.25': '*',
    '0.4': '*',
    '0.65': '*',
    '1.0': '*',
    '1.5': '*',
    '2.5': 5,
    '4.0': 3,
    '6.5': 2,
    '10.0': 2,
  },
  '9-15': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': '*',
    '0.1': '*',
    '0.15': '*',
    '0.25': '*',
    '0.4': '*',
    '0.65': '*',
    '1.0': 13,
    '1.5': 8,
    '2.5': 5,
    '4.0': 3,
    '6.5': 2,
    '10.0': 2,
  },
  '16-25': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': '*',
    '0.1': '*',
    '0.15': '*',
    '0.25': '*',
    '0.4': '*',
    '0.65': 20,
    '1.0': 13,
    '1.5': 8,
    '2.5': 5,
    '4.0': 3,
    '6.5': 3,
    '10.0': 2,
  },
  '26-50': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': '*',
    '0.1': '*',
    '0.15': '*',
    '0.25': '*',
    '0.4': 32,
    '0.65': 20,
    '1.0': 13,
    '1.5': 8,
    '2.5': 5,
    '4.0': 5,
    '6.5': 5,
    '10.0': 3,
  },
  '51-90': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': '*',
    '0.1': '*',
    '0.15': 80,
    '0.25': 50,
    '0.4': 32,
    '0.65': 20,
    '1.0': 13,
    '1.5': 8,
    '2.5': 7,
    '4.0': 6,
    '6.5': 5,
    '10.0': 4,
  },
  '91-150': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': '*',
    '0.1': 125,
    '0.15': 80,
    '0.25': 50,
    '0.4': 32,
    '0.65': 20,
    '1.0': 13,
    '1.5': 12,
    '2.5': 11,
    '4.0': 7,
    '6.5': 6,
    '10.0': 5,
  },
  '151-280': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': '*',
    '0.065': 200,
    '0.1': 125,
    '0.15': 80,
    '0.25': 50,
    '0.4': 32,
    '0.65': 20,
    '1.0': 20,
    '1.5': 19,
    '2.5': 13,
    '4.0': 10,
    '6.5': 7,
    '10.0': 6,
  },
  '281-500': {
    '0.01': '*',
    '0.015': '*',
    '0.025': '*',
    '0.04': 315,
    '0.065': 200,
    '0.1': 125,
    '0.15': 80,
    '0.25': 50,
    '0.4': 48,
    '0.65': 47,
    '1.0': 29,
    '1.5': 21,
    '2.5': 16,
    '4.0': 11,
    '6.5': 9,
    '10.0': 7,
  },
  '501-1200': {
    '0.01': '*',
    '0.015': 800,
    '0.025': 500,
    '0.04': 315,
    '0.065': 200,
    '0.1': 125,
    '0.15': 80,
    '0.25': 75,
    '0.4': 73,
    '0.65': 47,
    '1.0': 34,
    '1.5': 27,
    '2.5': 19,
    '4.0': 15,
    '6.5': 11,
    '10.0': 8,
  },
  '1201-3200': {
    '0.01': 1250,
    '0.015': 800,
    '0.025': 500,
    '0.04': 315,
    '0.065': 200,
    '0.1': 125,
    '0.15': 120,
    '0.25': 116,
    '0.4': 73,
    '0.65': 53,
    '1.0': 42,
    '1.5': 35,
    '2.5': 23,
    '4.0': 18,
    '6.5': 13,
    '10.0': 9,
  },
  '3201-10000': {
    '0.01': 1250,
    '0.015': 800,
    '0.025': 500,
    '0.04': 315,
    '0.065': 200,
    '0.1': 192,
    '0.15': 189,
    '0.25': 116,
    '0.4': 86,
    '0.65': 68,
    '1.0': 50,
    '1.5': 38,
    '2.5': 29,
    '4.0': 22,
    '6.5': 15,
    '10.0': 9,
  },
  '10001-35000': {
    '0.01': 1250,
    '0.015': 800,
    '0.025': 500,
    '0.04': 315,
    '0.065': 300,
    '0.1': 294,
    '0.15': 189,
    '0.25': 135,
    '0.4': 108,
    '0.65': 77,
    '1.0': 60,
    '1.5': 46,
    '2.5': 35,
    '4.0': 29,
    '6.5': 15,
    '10.0': 9,
  },
  '35001-150000': {
    '0.01': 1250,
    '0.015': 800,
    '0.025': 500,
    '0.04': 490,
    '0.065': 476,
    '0.1': 294,
    '0.15': 218,
    '0.25': 170,
    '0.4': 123,
    '0.65': 96,
    '1.0': 74,
    '1.5': 56,
    '2.5': 40,
    '4.0': 29,
    '6.5': 15,
    '10.0': 9,
  },
  '150001-500000': {
    '0.01': 1250,
    '0.015': 800,
    '0.025': 750,
    '0.04': 715,
    '0.065': 476,
    '0.1': 345,
    '0.15': 270,
    '0.25': 200,
    '0.4': 156,
    '0.65': 119,
    '1.0': 90,
    '1.5': 64,
    '2.5': 40,
    '4.0': 29,
    '6.5': 15,
    '10.0': 9,
  },
  '500001-up': {
    '0.01': 1250,
    '0.015': 1200,
    '0.025': 1112,
    '0.04': 715,
    '0.065': 556,
    '0.1': 435,
    '0.15': 303,
    '0.25': 244,
    '0.4': 189,
    '0.65': 143,
    '1.0': 102,
    '1.5': 64,
    '2.5': 40,
    '4.0': 29,
    '6.5': 15,
    '10.0': 9,
  },
}

// 获取适用的批量范围
const getLotSizeRange = (size: number): string | null => {
  const range = LOT_SIZE_RANGES.find((r) => size >= r.min && size <= r.max)
  return range?.code ?? null
}

// 计算样本量
const calculateSampleSize = () => {
  const { lotSize, aqlValue } = state

  if (!aqlValue || !lotSize) {
    state.sampleSize = 0
    state.isFullInspection = false
    return
  }

  const range = getLotSizeRange(lotSize)
  if (!range) {
    state.sampleSize = lotSize
    state.isFullInspection = true
    return
  }

  const sampleValue = samplingTable[range][aqlValue.toString()]
  if (sampleValue === '*') {
    state.sampleSize = lotSize
    state.isFullInspection = true
  } else {
    state.sampleSize = sampleValue
    state.isFullInspection = false
  }
}

// 监听输入值变化
watch(
  () => [state.lotSize, state.aqlValue],
  () => calculateSampleSize(),
  { immediate: true },
)

// 计算结果的文本显示
const resultText = computed(() => {
  if (!state.lotSize || !state.aqlValue) return '请输入必要参数'
  return `${state.sampleSize} 件${state.isFullInspection ? ' (需要全检)' : ''}`
})
</script>

<template>
  <div class="p-6">
    <h2 class="text-2xl font-bold mb-6">零缺陷抽样计算器 (C=0)</h2>

    <div class="space-y-4 max-w-md">
      <!-- 生产数量输入框 -->
      <n-form-item label="生产数量">
        <n-input-number
          v-model:value="state.lotSize"
          :min="2"
          placeholder="请输入生产总数量"
          clearable
        />
      </n-form-item>

      <!-- AQL值下拉选择 -->
      <n-form-item label="AQL值">
        <n-select
          v-model:value="state.aqlValue"
          :options="aqlOptions"
          placeholder="请选择AQL值"
          clearable
        />
      </n-form-item>

      <!-- 计算结果显示 -->
      <n-card title="计算结果" class="mt-6">
        <template #header-extra>
          <n-tooltip trigger="hover">
            <template #trigger>
              <n-icon size="20">
                <QuestionCircleOutline />
              </n-icon>
            </template>
            基于GB/T2828.1-2003标准零缺陷抽样方案(C=0)
          </n-tooltip>
        </template>

        <div class="text-lg">
          需要抽检的样品数量：
          <span class="font-bold text-primary">{{ resultText }}</span>
        </div>

        <div class="mt-4 text-sm text-gray-500">
          <p>* 当发现1个或以上不合格品时，则判定该批次不合格</p>
          <p>* 样本量基于GB/T2828.1-2003标准抽样表</p>
          <p>* 适用批量范围：2 ~ ∞</p>
        </div>
      </n-card>
    </div>
  </div>
</template>

<style scoped>
.n-form-item {
  max-width: 300px;
}
</style>
